package com.ycz;

import com.ycz.mapper.UserMapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;

import java.util.Map;
import java.util.Set;

/**
 * @author ycz
 * @date 2021/6/18
 *
 * 将mapper接口代理类注册到spring中
 */
public class MapperBeanDefinitionRegistry implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
        AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
        // 设置beanClass
        beanDefinition.setBeanClass(MapperFactoryBean.class);
        // 设置注册beanDefinition（MapperFactoryBean）的构造方法
        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(UserMapper.class);
        // 注册userMapper bean
        registry.registerBeanDefinition("userMapper",beanDefinition);
    }


}
